package com.guanzhi.controller;

import com.guanzhi.annotation.MethodCallCountAnnotation;
import com.guanzhi.base.BaseController;
import com.guanzhi.emnu.AlgorithmMode;
import com.guanzhi.po.Personnel;
import com.guanzhi.restful.Response;
import com.guanzhi.service.UserService;
import com.guanzhi.utils.AesSecurityUtil;
import io.micrometer.common.util.StringUtils;
import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * SpringBoot 实战
 * <p> Package: com.guanzhi.controller </p>
 * <p> File: UserController.java </p>
 * <p> Description:  (用户控制器)</p>
 * <p> Date: 2024/2/5 18:30</p>
 *
 * @author 观之
 * @version 1.0 （代码版本）
 * @email <a href="mailto:guanzhi55634@aliyun.com">观之</a>（邮箱）
 * @date 2024/2/5 18:30
 * @since jdk17（jdk版本）
 */
@Validated
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController extends BaseController {

    private UserService userService;

    /**
     * 注册
     *
     * @param username 用户名
     * @param password 密码
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Response userRegister(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return error("错误操作,参数为空,请联系管理员检查！");
        }
        Personnel personnel = userService.findPersonnelByUsername(username);
        if (personnel!=null){
            return error("当前用户已存在,请重试！");
        }

        userService.register(username, password);
        return success("注册成功");
    }

    /**
     * 根据id查询用户
     *
     * @param id 用户id
     */
    @GetMapping("/getPersonnelById")
    public Response getPersonnelById(Long id) {
        if (id == null) {
            return error("入参");
        }
        Personnel personnelById = userService.findPersonnelById(id);
        if (personnelById == null) {
            return error("用户不存在");
        }
        return success(personnelById);
    }

    /**
     * 根据用户名查询用户
     *
     * @param username 用户名
     */
    @GetMapping("/getPersonnelByUsername")
    public Response getPersonnelByUsername(String username) {
        Personnel personnelByUsername = userService.findPersonnelByUsername(username);
        if (personnelByUsername == null) {
            return error("用户不存在");
        }
        return success(personnelByUsername);
    }

    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Response login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return error("错误操作,请联系管理员检查！");
        }

        //通过用户名查询,获取人员对象
        Personnel personnel = userService.findPersonnelByUsername(username);
        if (personnel == null) {
            return error("用户不存在");
        }

        //加密密码
        String encryptPassword = personnel.getPassword();
        String decryptPassword = AesSecurityUtil.AES_CBC(encryptPassword, AlgorithmMode.decrypt.getMode());
        if (StringUtils.isBlank(decryptPassword)) {
            return error("密码解密失败!");
        }

        if (decryptPassword.equals(password)) {
            return success("登录成功,JWT令牌","");
        }

        return error("密码错误");
    }
}
